//
//  MGCNavigationBar.m
//  MiguDM
//
//  Created by XXX on 2017/6/26.
//  Copyright © 2017年 咪咕动漫. All rights reserved.
//

#import "PTNavigationBar.h"
#import "PTViewController.h"

#import "UIImage+YYAdd.h"
#import "YYCGUtilities.h"
#import <Masonry/Masonry.h>
#import "UIDevice+MGAdd.h"

@interface PTNavigationBar()

@property (nonatomic, strong) UIImageView *backgroundView;

@property (nonatomic, strong)UILabel *titleLabel;

@property (nonatomic, strong) UIButton *rightButton2;//右边右起第二个按钮，rightButton 的左边
//排列如：rightButton2 rightButton

@property (nonatomic, strong) UIButton *leftButton1;

@property (nonatomic, strong) UIButton *leftButton2;

@end

@implementation PTNavigationBar

- (instancetype)init {
    self = [super init];
    if (self) {
        [self addSubview:self.titleLabel];
        [self addSubview:self.backgroundView];
        [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self);
            make.height.mas_equalTo(44);
            make.left.equalTo(self).offset(50);
            make.right.equalTo(self).offset(-50);
        }];
    }
    return self;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}

- (void)setBackButton:(UIButton *)backButton {
    if (_leftButton1) {
        [_leftButton1 removeFromSuperview];
        _leftButton1 = nil;
    }
    if (_leftButton2) {
        [_leftButton2 removeFromSuperview];
        _leftButton2 = nil;
    }
    if (backButton) {
        _backButton = backButton;
    }
    //设置默认返回按钮
    if (_backButton == nil) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setImage:UINavigationBar.appearance.backIndicatorImage forState:UIControlStateNormal];
        button.tintColor = [UIColor whiteColor];
        button.frame = CGRectMake(0, 20 + iPhoneXYOffset, 60, 44);
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 7.5, 0, 0);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [button addTarget:self action:@selector(popSelf) forControlEvents:UIControlEventTouchUpInside];
        _backButton = button;
    }
    [self addSubview:_backButton];
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(60);
    }];
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(50);
    }];
}

- (void)popSelf {
    [self.viewController.navigationController popViewControllerAnimated:YES];
}

- (void)setRightButton:(UIButton *)rightButton {
    if (_rightButton) {
        [_rightButton removeFromSuperview];
    }
    if (_rightButton2) {
        [_rightButton2 removeFromSuperview];
    }
    if (rightButton) {
        _rightButton = rightButton;
        [self addSubview:_rightButton];
        [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.titleLabel);
            make.right.equalTo(self).offset(-5);
            make.width.mas_equalTo(44);
            make.height.mas_equalTo(44);
        }];
    } else {
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-50);
        }];
    }
}

- (void)setRightButtons:(NSArray *)rightButtons {
    if (_rightButton) {
        [_rightButton removeFromSuperview];
        _rightButton = nil;
    }
    if (_rightButton2) {
        [_rightButton2 removeFromSuperview];
        _rightButton2 = nil;
    }
    if (rightButtons.count >= 1) {
        UIButton *rightButton = rightButtons[0];
        if ([rightButton isKindOfClass:UIButton.class]) {
            //添加最右边按钮
            _rightButton = rightButton;
            [self addSubview:_rightButton];
            [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.titleLabel);
                make.right.equalTo(self).offset(-5);
                make.width.mas_equalTo(44);
                make.height.mas_equalTo(44);
            }];
            
            if (rightButtons.count >= 2) {
                UIButton *rightButton2 = rightButtons[1];
                if ([rightButton2 isKindOfClass:UIButton.class]) {
                    //添加右2按钮
                    _rightButton2 = rightButton2;
                    [self addSubview:_rightButton2];
                    [_rightButton2 mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.centerY.equalTo(self.titleLabel);
                        make.right.mas_equalTo(self.rightButton.mas_left);
                        make.width.mas_equalTo(44);
                        make.height.mas_equalTo(44);
                    }];
                }
            }
        }
    }
    
//    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//        if (self.rightButton2) {
//            make.right.equalTo(self).offset(-100);
//        } else {
//            make.right.equalTo(self).offset(-50);
//        }
//    }];
}

- (void)setLeftButtons:(NSArray *)leftButtons {
    if (_leftButton1) {
        [_leftButton1 removeFromSuperview];
        _leftButton1 = nil;
    }
    if (_leftButton2) {
        [_leftButton2 removeFromSuperview];
        _leftButton2 = nil;
    }
    
    if (leftButtons.count <= 0) {
        [self setBackButton:self.backButton];
    } else {
        
        if (leftButtons.count >= 1) {
            if (_backButton) {
                [_backButton removeFromSuperview];
            }
            
            UIButton *button = leftButtons[0];
            if ([button isKindOfClass:UIButton.class]) {
                //添加最左边按钮
                _leftButton1 = button;
                [self addSubview:_leftButton1];
                [_leftButton1 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self.titleLabel);
                    make.left.equalTo(self).offset(0);
                    make.width.mas_equalTo(44);
                    make.height.mas_equalTo(44);
                }];
                
                if (leftButtons.count >= 2) {
                    UIButton *button2 = leftButtons[1];
                    if ([button2 isKindOfClass:UIButton.class]) {
                        //添加右2按钮
                        _leftButton2 = button2;
                        [self addSubview:_leftButton2];
                        [_leftButton2 mas_makeConstraints:^(MASConstraintMaker *make) {
                            make.centerY.equalTo(self.titleLabel);
                            make.left.mas_equalTo(self.leftButton1.mas_right);
                            make.width.mas_equalTo(44);
                            make.height.mas_equalTo(44);
                        }];
                    }
                }
            }
        }
        
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            if (self.leftButton2) {
                make.left.equalTo(self).offset(100);
            } else {
                make.left.equalTo(self).offset(50);
            }
        }];
    }
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = UINavigationBar.appearance.titleTextAttributes[NSForegroundColorAttributeName];
        _titleLabel.font = [UIFont mediumPingFangFontOfSize:15];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIImageView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIImageView alloc] init];
        _backgroundView.image = [UIImage imageWithColor:UINavigationBar.appearance.barTintColor];
    }
    return _backgroundView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self sendSubviewToBack:self.backgroundView];
}

@end
